package com.example.myviewmodelframe.base.model

import android.app.Application
import android.util.Log
import androidx.lifecycle.ViewModel
import java.lang.Exception
import java.lang.reflect.ParameterizedType
/**带参数的viewmodel*/
class BaseViewModelParamFactory<R:Any>(

    private val context: Application,
    private val repository: R
) : BaseDefaultFactory() {

    companion object {
        private const val TAG = "ViewModelFactory"
    }

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        if (BaseViewModel::class.java.isAssignableFrom(modelClass)) {
            try {
                val classR = repository::class.java
                return modelClass.getConstructor(Application::class.java, classR)
                    .newInstance(context, repository)
            } catch (e: Exception) {
                Log.e(TAG, "exception == ${e.message}")
            }
        }
        return super.create(modelClass)
    }
}